﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Represents dropdown list hierarchy that holds information about Teams and Users
/// </summary>
public partial class TeamUserSelection : System.Web.UI.UserControl
{
    /// <summary>
    /// Gets the selected User.
    /// </summary>
    /// <value>The selected User.</value>
    public User SelectedUser 
    {
        get 
        {
            if (String.IsNullOrEmpty(this.DropDownListUsers.SelectedValue))
            {
                return null;
            }
            else
            {
                User usr = new User();
                usr.ID = System.Convert.ToInt32(this.DropDownListUsers.SelectedValue);
                return usr;
            }
        } 
    }

    /// <summary>
    /// Gets the selected team.
    /// </summary>
    /// <value>The selected team.</value>
    public Team SelectedTeam
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListTeam.SelectedValue))
            {
                return null;
            }
            else
            {
                return DAOTeam.GetTeam(Int32.Parse(this.DropDownListTeam.SelectedValue));
            }
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    /// <summary>
    /// Loads the Team.
    /// </summary>
    protected void LoadTeams()
    {
        DropDownListTeam.DataSource = DAOTeam.GetAllTeams();
        DropDownListTeam.DataTextField = "Name";
        DropDownListTeam.DataValueField = "Id";
        DropDownListTeam.DataBind();
        DropDownListTeam.Items.Insert(0, new ListItem((string)GetLocalResourceObject("ChooseTeam"), String.Empty));
    }

    /// <summary>
    /// Loads the users.
    /// </summary>
    protected void LoadUsers()
    {
        if (String.IsNullOrEmpty(DropDownListTeam.SelectedValue))
        {
            DropDownListUsers.DataSource = DAOUser.GetTeamMembers();
        }
        else
        {
            DropDownListUsers.DataSource = DAOUser.GetTeamMembers(DAOTeam.GetTeam(Int32.Parse(DropDownListTeam.SelectedValue)));
        }
        DropDownListUsers.DataTextField = "FullName";
        DropDownListUsers.DataValueField = "ID";
        DropDownListUsers.DataBind();
        DropDownListUsers.Items.Insert(0, new ListItem((string)GetLocalResourceObject("ChooseUser"), String.Empty));
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        LoadTeams();
        LoadUsers();
    }

    /// <summary>
    /// Selected team changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedTeamChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadUsers();
        }
    }

    /// <summary>
    /// Selected user changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedUserChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedUser == null)
            {
                return;
            }
            int selectedUserID = this.SelectedUser.ID;
            Team team = this.SelectedUser.Team[0];

            // Select the right team
            for (int i = 0; i < this.DropDownListTeam.Items.Count; i++)
            {
                if (this.DropDownListTeam.Items[i].Value == team.ID.ToString())
                {
                    this.DropDownListTeam.SelectedIndex = i;
                    break;
                }
            }
            // Reload users
            LoadUsers();

            // Select the right user
            for (int i = 0; i < this.DropDownListUsers.Items.Count; i++)
            {
                if (this.DropDownListUsers.Items[i].Value == selectedUserID.ToString())
                {
                    this.DropDownListUsers.SelectedIndex = i;
                    break;
                }
            }
        }
    }

    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear()
    {
        DataBind();
        this.DropDownListTeam.SelectedIndex = 0;
        this.DropDownListUsers.SelectedIndex = 0;
    }
                
}
